<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 头部 -->
        <tree-tools
          :is-root="true"
          :node-data="company"
          @add-depts="handleAddDepts"
        />
        <!-- 内容部分 -->
        <!-- default-expand-all   是否默认展开所以的节点 -->
        <el-tree
          :default-expand-all="true"
          :data="departs"
          :props="defaultProps"
        >
          <template #default="{ data }">
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
        <!-- 添加的对话框 -->
        <add-dept
          ref="addDeptDialog"
          :show-dialog="showDialog"
          :node-data="nodeData"
          :depts-list="deptsList"
          @close-dialog="handleCloseDialog"
          @add-depts="getDepartments"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept' // 引入新增部门组件
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'

export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      loading: false,
      // label本节点文字，children子节点列表
      company: {
        name: '',
        manager: '负责人'
      },

      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 控制显示隐藏的
      showDialog: false,
      // 这一个对象，欧诺个鱼标记正在操作哪个节点
      nodeData: {},
      // 将得到的列表时的数据， 传给子组件用于判断
      deptsList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loding = true
      const { data } = await reqGetDepartments()
      this.company.name = data.companyName
      this.company.manager = data.companyManage || '负责人'
      this.company.id = '' // 将来以'' 作为pid, 找子部门，找到所以的一级部门

      // 存一下列表式的数据
      this.deptsList = data.depts
      this.departs = tranListToTreeData(data.depts, '') // 需要将其转换成树形结
      this.loding = false
    },
    handleCloseDialog(flag) {
      this.showDialog = flag
    },
    handleAddDepts(nodeData) {
      // 1显示弹框
      this.showDialog = true
      // 2存好nodeData (标记，正在往谁下面添加子节点)
      this.nodeData = nodeData
      // 3 希望显示弹框的那一刻，立刻发送请求获取负责人列表   ref $refs
      this.$refs.addDeptDialog.getEmployeesList()
      // this.$refs.addDeptDialog.showTitle = '添加弹框'
    },
    handleEditDepts(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData // 这次存的是正在这编辑的是谁
      // 获取负责人列表
      this.$refs.addDeptDialog.getEmployeesList()
      // 根据id发送请求，获取到部门信息，进行回显
      this.$refs.addDeptDialog.getDeparmentDetail(this.nodeData.id)
      // this.$refs.addDeptDialog.showTitle = '修改弹框'
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;

  ::v-deep {
    .el-tree-node__content {
      height: 36px;
    }
    // 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
